/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PainelConfiguracao.java
 *
 * Created on May 31, 2011, 3:37:49 PM
 */
package cataletra.gui;

import cataletra.main.Main;
import cataletra.db.DB;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author arthur
 */
public class PainelConfiguracao extends javax.swing.JPanel {

    private DB db;
    
    /** Creates new form PainelConfiguracao */
    public PainelConfiguracao() {
        initComponents();
        
        db = new DB();
        
        reiniciar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        painelCombos = new javax.swing.JPanel();
        comboTema = new javax.swing.JComboBox();
        comboNivel = new javax.swing.JComboBox();
        labelTema = new javax.swing.JLabel();
        labelNivel = new javax.swing.JLabel();
        painelBotao = new javax.swing.JPanel();
        botaoJogar = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        painelCombos.setBorder(javax.swing.BorderFactory.createEmptyBorder(30, 1, 1, 1));
        painelCombos.setLayout(new java.awt.GridBagLayout());

        comboTema.setPreferredSize(new java.awt.Dimension(73, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        painelCombos.add(comboTema, gridBagConstraints);

        comboNivel.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FÁCIL", "MÉDIO", "DIFÍCIL", "EXPERT" }));
        comboNivel.setSelectedIndex(-1);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 100;
        gridBagConstraints.insets = new java.awt.Insets(3, 5, 5, 5);
        painelCombos.add(comboNivel, gridBagConstraints);

        labelTema.setText("Tema");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        painelCombos.add(labelTema, gridBagConstraints);

        labelNivel.setText("Nível");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        painelCombos.add(labelNivel, gridBagConstraints);

        add(painelCombos, java.awt.BorderLayout.PAGE_START);

        painelBotao.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 20, 1));

        botaoJogar.setFont(new java.awt.Font("Dialog", 0, 20));
        botaoJogar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/start.png"))); // NOI18N
        botaoJogar.setText("Jogar");
        botaoJogar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoJogar(evt);
            }
        });
        painelBotao.add(botaoJogar);

        add(painelBotao, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void clicarBotaoJogar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoJogar
        JanelaPrincipal janelaPrincipal = Main.getJanelaPrincipal();

        if(comboTema.getSelectedIndex() == -1){
            JOptionPane.showMessageDialog(janelaPrincipal, "Escolha um tema!", "Atenção!", JOptionPane.WARNING_MESSAGE);
            return;
        }            
        
        Object[] p = db.getPalavras((String) comboTema.getSelectedItem());
        ArrayList palavras = new ArrayList();
        palavras.addAll(Arrays.asList(p));

        //0 = largura 1 = altura 2 = qtdeLinhas, 3 = qtdeColunas e 4 = qtdePalavras
        int[] dados = new int[5];


        int dificuldade = comboNivel.getSelectedIndex();
        String tema = (String) comboTema.getSelectedItem();

        if(dificuldade == 0){
            dados = new int[]{720, 540, 10, 10, 10};
        } else if(dificuldade == 1){
            dados = new int[]{850, 600, 12, 17, 14};
        } else if(dificuldade == 2){
            dados = new int[]{960, 710, 16, 20, 18};
        } else if(dificuldade == 3){
            dados = new int[]{1000, 800, 18, 25, 22};
        } else {
            JOptionPane.showMessageDialog(janelaPrincipal, "Escolha um nível de dificuldade!", "Atenção!", JOptionPane.WARNING_MESSAGE);
            return;
        }

        if(palavras.size() < dados[4]){
            JOptionPane.showMessageDialog(janelaPrincipal, "Este tema não possui palavras suficientes!",
                    "Atenção!", JOptionPane.WARNING_MESSAGE);
        } else {
            janelaPrincipal.setSize(new Dimension(dados[0], dados[1]));
            janelaPrincipal.iniciarJogo(tema, palavras, dados[2], dados[3], dados[4]);
        }

        
    }//GEN-LAST:event_clicarBotaoJogar

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoJogar;
    private javax.swing.JComboBox comboNivel;
    private javax.swing.JComboBox comboTema;
    private javax.swing.JLabel labelNivel;
    private javax.swing.JLabel labelTema;
    private javax.swing.JPanel painelBotao;
    private javax.swing.JPanel painelCombos;
    // End of variables declaration//GEN-END:variables

    
    protected final void reiniciar(){
        Object[] temas = db.getTemas();
        Object[] listaTemas = new Object[temas.length + 1];        
        System.arraycopy(temas, 0, listaTemas, 1, temas.length); //copia os dados de tema para listaTemas
        listaTemas[0] = "TODOS";

        comboTema.setModel(new DefaultComboBoxModel(listaTemas));
        comboTema.setSelectedIndex(-1);
        comboNivel.setSelectedIndex(-1);
    }
}
